package com.neouton.crm.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.approval.entity.ApprovalVO;
import com.neouton.crm.product.entity.ProductInfoVO;
import com.neouton.crm.product.service.IProductInfoServer;
import com.neouton.crm.supplier.entity.SupplierVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author libingkun
 * @Date 2022-11-14
 * @Version 1.0
 *          初始化迁移
 */
@RestController
@RequestMapping("/productInfo")
public class ProductInfoController {

    @Resource
    private IProductInfoServer productInfoServer;

    @PostMapping("/queryProductsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryProductsByPage( @PathVariable(value = "pageNum") int pageNum,
                                            @PathVariable(value = "pageSize") int pageSize,
                                            @RequestBody ProductInfoVO entity) {
        IPage<ProductInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ProductInfoVO> result = productInfoServer.queryProductsByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 数据保存
     *
     * @return
     */
    @PostMapping(value = "/saveProduct")
    public ResponseAPI saveProduct(@RequestHeader("token") String token, @RequestBody ProductInfoVO productInfoVO) {
        Boolean flage = productInfoServer.saveOrUpdateProduct(productInfoVO,token);
        return ResponseAPI.successData(flage);
    }

    /**
     * 数据删除
     *
     * @param userId
     * @return
     */
    @GetMapping("/delete/{userId}")
    public ResponseAPI delete(@PathVariable("userId") Integer userId) {
        return ResponseAPI.successMes("删除数据成功");
    }


    @PostMapping("/exportProductInfo")
    public void productInfosExport(HttpServletResponse response,@RequestBody ProductInfoVO entit) throws Exception{
        productInfoServer.exportProductInfo(response,entit,"");
    }

    @PostMapping("/exportOneFingerProductInfo")
    public void exportOneFingerProductInfo(HttpServletResponse response,@RequestBody ProductInfoVO entit) throws Exception{
        productInfoServer.exportProductInfo(response,entit,"一指禅");
    }


    @PostMapping("/queryAllProductsForPur")
    public ResponseAPI queryAllProductsForPur(@RequestBody ProductInfoVO productInfoVO){
        return ResponseAPI.successData( productInfoServer.queryAllProductsForPur(productInfoVO));
    }

    @PostMapping("/querySuppliers")
    public ResponseAPI querySuppliers(){
        return ResponseAPI.successData( productInfoServer.querySuppliers());
    }

    /**
     * 查询全部供应商
     *
     * @return
     */
    @PostMapping("/queryAllSuppliers")
    public ResponseAPI queryAllSuppliers(){
        return ResponseAPI.successData( productInfoServer.queryAllSuppliers());
    }

    /**
     * 审批通过
     * @param approval
     * @return
     */
    @PostMapping("/approveProductInfo")
    public ResponseAPI approveProductInfo(@RequestHeader("token") String token, @RequestBody ApprovalVO approval){
        return ResponseAPI.successData( productInfoServer.approveProductInfo(approval,token,true));
    }

    /**
     * 审批驳回
     * @param approval
     * @return
     */
    @PostMapping("/rejectionProductInfo")
    public ResponseAPI rejectionProductInfo(@RequestHeader("token") String token, @RequestBody ApprovalVO approval){
        return ResponseAPI.successData( productInfoServer.rejectionProductInfo(approval,token));
    }

    /**
     * 更具工作流ID获取审批信息
     * @param workId
     * @return
     */
    @PostMapping("/querProductApprovalByWorkId/{workId}")
    public ResponseAPI querProductApprovalByWorkId(@PathVariable("workId") Integer workId){
        return ResponseAPI.successData( productInfoServer.querProductApprovalByWorkId(workId));
    }

    /**
     * 更具工作流ID获取产品列表
     * @param approvalId
     * @return
     */
    @PostMapping("/queryTodoProductsByApprovalId/{approvalId}")
    public ResponseAPI queryTodoProductsByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( productInfoServer.queryTodoProductsByApprovalId(approvalId));
    }

    /**
     * 更具工作流ID获取产品列表
     * @param approvalId
     * @return
     */
    @PostMapping("/queryProductApprovalRecordByApprovalId/{approvalId}")
    public ResponseAPI queryProductApprovalRecordByApprovalId(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( productInfoServer.queryProductApprovalRecordByApprovalId(approvalId));
    }

    /**
     * 产品导入功能
     * @param file
     * @param supperId
     * @param reason
     * @return
     */
    @PostMapping("/importProducts")
    public ResponseAPI importProducts(@RequestParam("file") MultipartFile file,
                                      @RequestParam("supplierId") Integer supperId,
                                      @RequestParam("isOneFinger") Integer isOneFinger,
                                      @RequestParam("reason") String reason,
                                      @RequestHeader("token") String token) throws Exception{
        return ResponseAPI.successData(productInfoServer.importProducts(file,supperId,isOneFinger,reason,token));
    }


    @PostMapping("/downProductTemplate")
    public void downProductTemplate(HttpServletResponse response) throws Exception {
        productInfoServer.downProductTemplate(response);
    }

    /**
     * 变更启用、禁用状态
     * @param productInfoVO
     * @return
     */
    @PostMapping("/updateProductState")
    public ResponseAPI updateProductState( @RequestBody ProductInfoVO productInfoVO){
        Boolean flage = productInfoServer.updateProductState(productInfoVO);
        return ResponseAPI.successData(flage);
    }


    /**
     * 分页查询产品待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalProductByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalProductInfoByPage( @PathVariable(value = "pageNum") int pageNum,
                                               @PathVariable(value = "pageSize") int pageSize,
                                               @RequestBody ProductInfoVO entity) {
        IPage<ProductInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ProductInfoVO> result = productInfoServer.queryApprovalProductByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询产品已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalProductInfoDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalProductInfoDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                                   @PathVariable(value = "pageSize") int pageSize,
                                                   @RequestBody ProductInfoVO entity) {
        IPage<ProductInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ProductInfoVO> result = productInfoServer.queryApprovalProductDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询产品申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalProductInfoRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalProductInfoRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                     @PathVariable(value = "pageSize") int pageSize,
                                                     @RequestBody ProductInfoVO entity,
                                                     @RequestHeader("token") String token) {
        IPage<ProductInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ProductInfoVO> result = productInfoServer.queryApprovalProductRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }

}
